package com.wtwd.campus.service.impl;

import com.wtwd.campus.common.Constants;
import com.wtwd.campus.dao.LocationMapper;
import com.wtwd.campus.entity.LocationData;
import com.wtwd.campus.service.*;
import com.wtwd.campus.vcom.entity.VcomLocationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class LocationServiceImpl implements LocationService {
    @Autowired
    private LocationMapper locationMapper;

    @Autowired
    private RedisService redisService;

    @Resource
    private LastLocationDataService lastLocationDataService;

    @Resource
    private DeviceFlagService deviceFlagService;

    @Override
    public List<Map<String, Object>> getClassesLocationList(Integer classId, String imei, String orgCode) {
        List<Map<String, Object>> locationMap =lastLocationDataService.getClassesLocationListByClassOrImei(classId,imei,orgCode);
        List<String> imeis = locationMap.stream().map(e -> (String) e.get("imei")).collect(Collectors.toList());
        if (imeis!=null&&imeis.size()>0){
            Map<String, Boolean> stringBooleanMap = deviceFlagService.bathGetDeviceOnline(imeis);
            for (Map<String, Object> map : locationMap) {
                Boolean online = stringBooleanMap.get((String) map.get("imei"));
                if(online){
                    map.put("on_off",1);//在线
                }else{
                    map.put("on_off",2);//离线
                }

            }
        }
        return locationMap;
    }

    @Override
    public List<Map<String, Object>> getDeviceTrack(String key, Long time, String orgCode) {
        Long maxTime = time + 24 * 60 * 60 * 1000;
        List<Map<String, Object>> deviceTrack = locationMapper.getDeviceTrack(key, time, maxTime, orgCode);
        if (deviceTrack != null && deviceTrack.size() > 0) {
            Map<String, Object> map1 = deviceTrack.get(0);
            String deviceImei = (String) map1.get("imei");
            //判读用户是否在线
            boolean b = redisService.hasKey(Constants.DEVICE_ONLINE_PREFIX.concat(deviceImei));
            for (Map<String, Object> map : deviceTrack) {
                if (b) {
                    map.put("on_off", 1);//在线
                } else {
                    map.put("on_off", 2);//离线
                }
            }
        }
        return deviceTrack;
    }

    @Override
    public Integer addLocationData(LocationData data) {
        return locationMapper.addLocationData(data);
    }

    @Override
    public LocationData getLatestLocationData(Integer deviceId) {
        return locationMapper.getLatestLocationData(deviceId);
    }

    @Override
    public List<Map<String, Object>> getDeviceTrackByDeviceId(Integer deviceId, Long time) {
        Long maxTime = 24 * 60 * 60 * 1000 + time;
        return locationMapper.getDeviceTrackByDeviceId(deviceId, time, maxTime);
    }

    @Override
    public List<Map<String, Object>> getDeviceTrackByDeviceIdWx(Integer deviceId, Long time) {
        Long maxTime = 24 * 60 * 60 * 1000 + time;
        return locationMapper.getDeviceTrackByDeviceIdWx(deviceId, time, maxTime);
    }

    @Override
    public VcomLocationData getVcomLatestLocationData(Integer deviceId) {
        return locationMapper.getVcomLatestLocationData(deviceId);
    }

    @Override
    public List<Map<String, Object>> getDeviceTrackByDeviceIdVcom(Integer deviceId, Long time) {
        Long maxTime = 24 * 60 * 60 * 1000 + time;
        return locationMapper.getDeviceTrackByDeviceIdVcom(deviceId, time, maxTime);
    }

    @Override
    public List<LocationData> getLocationByTime(Integer deviceId, long startTime, long endTime) {
        return locationMapper.getLocationByTime(deviceId, startTime, endTime);
    }

    @Override
    public List<LocationData> getLocationByDeviceId(Integer deviceId) {
        return locationMapper.getLocationByDeviceId(deviceId);
    }

    @Override
    public boolean deleteLocationDateByTime(long time) {
        if (time==0){
            return false;
        }
        return locationMapper.deleteLocationDateByTime(time)>0?true:false;
    }
}
